package com.yantu.controller.student;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yantu.bean.User;
import com.yantu.mapper.UserMapper;
import com.yantu.util.JWTUtil;
import com.yantu.util.Result;
import com.yantu.view.student.StudentUserView;
import com.yantu.view.UserView;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-07
 */
@CrossOrigin
@RestController
@RequestMapping("/student")
@ApiModel("学生端接口")
public class StudentLoginController {
    @Autowired
    UserMapper userMapper;

    //登录

    @ApiOperation("web端用户登录")
    @PostMapping("/logins")
    public Result studentLogin(@ModelAttribute UserView user1) {
        //获取账号,获取密码
        String userName = user1.getUsername();
        String password = user1.getPassword();
        String token;
        //向数据库查询
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("user_name", userName)
                .eq("password", password);
        User user = userMapper.selectOne(query);

        System.out.println("在这里啦");
        System.out.println(user);
        if (user != null) {
            if (password.equals(user.getPassword())) { //一个简单的登录逻辑
                //给用户设置令牌的有效时间
                User jwtUser = JWTUtil.setTime(user);
                //将用户信息存到redis中
//                RedisUtils.saveValue(jwtUser.getId() + "", jwtUser, 30); //将用户信息存入redis数据库 第三和第四个参数为有效时间和时间单位
                //map用来存储放在payload中的信息有用户名和用户id
                Map<String, Object> userPayload = new HashMap<String, Object>();
                userPayload.put("id", jwtUser.getId());
                userPayload.put("username", jwtUser.getUserName());
                token = JWTUtil.createJwtTokenpc(userPayload); //使用工具类生成token
                System.out.println("获取到token了");
                user.setToken(token);
                System.out.println(user);
                UpdateWrapper updateQuery = new UpdateWrapper<>();
                updateQuery.eq("id",user.getId());
                updateQuery.set("loginTime",user.getLogintime());
                updateQuery.set("expireTime",user.getExpiretime());
                updateQuery.set("token",token);
                int updateNum = userMapper.update(null,updateQuery);//token存入表
                System.out.println(updateNum);
                System.out.println(token);
                Map<String,Object> map = new HashMap<>();
                map.put("username",user.getUserName());
                map.put("imagePath",user.getImagePath());
                return Result.succ(200, "登录成功", map);
            } else {
                return Result.fail("登录失败111");
            }
        } else {
            System.out.println("niyayayya");
            return Result.fail("登录失败222");
        }
    }

    //注册
    @PostMapping("/studentRegister")
    @ApiOperation("学生用户注册")
    public Result wxRegister(@RequestBody StudentUserView studentUserView){
        User user = new User();
        user.setPassword(studentUserView.getPassword());
        user.setUserName(studentUserView.getUsername());
        user.setUserLevel(studentUserView.getUserLevel());
        user.setRole(1);
        user.setStatus(1);
        user.setDeleted(true);
        int result = userMapper.insert(user);
        if(result>0){
            return Result.succ(200,"注册成功",null);
        }else {
            return Result.fail("注册失败");
        }
    }

    @PostMapping("/studentLoginOut")
    @ApiOperation("学生用户退出,功能未完善")
    public Result studentLoginOut(){
        User user = new User();
        int result = userMapper.insert(user);
        if(result>0){
            return Result.succ(200,"注册成功",null);
        }else {
            return Result.fail("注册失败");
        }
    }

    @PostMapping("/scanLogin/{scanToken}")
    @ApiOperation("微信扫码登录")
    public Result scanLogin(@PathVariable String scanToken){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scan_token",scanToken);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            return Result.succ("登录成功",user);
        }else {
            return Result.fail("登录失败");
        }
    }

}
